In [75]:
s = 'hello world'
In [76]:
# Capitalize first word in string
s.capitalize()
Out[76]:
In [77]:
s.upper()
Out[77]:
In [78]:
s.lower()
Out[78]:
In [80]:
s.count('o')
Out[80]:
In [81]:
s.find('o')
Out[81]:
In [83]:
s.center(20,'z')
Out[83]:
expandtabs() will expand tab notations \t into spaces:
In [84]:
'hello\thi'.expandtabs()
Out[84]:
In [40]:
s = 'hello'
isalnum() will return True if all characters in S are alphanumeric
In [41]:
s.isalnum()
Out[41]:
isalpha() wil return True if all characters in S are alphabetic
In [43]:
s.isalpha()
Out[43]:
islower() will return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise.
In [44]:
s.islower()
Out[44]:
isspace() will return True if all characters in S are whitespace.
In [45]:
s.isspace()
Out[45]:
istitle() will return True if S is a titlecased string and there is at least one character in S, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise.
In [47]:
s.istitle()
Out[47]:
isupper() will return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise.
In [35]:
s.isupper()
Out[35]:
Another method is endswith() which is essentially the same as a boolean check on s[-1]
In [69]:
s.endswith('o')
Out[69]:
Strings have some built-in methods that can resemble regular expression operations. We can use split() to split the string at a certain element and return a list of the result. We can use partition to return a tuple that includes the seperator (the first occurence) and the first half and the end half.
In [52]:
s.split('e')
Out[52]:
In [72]:
s.partition('e')
Out[72]:
In [58]:
s
Out[58]:
Great! You should now feel comfortable using the variety of methods that are built-in string objects!